package com.woniu.intercepters;

import com.woniu.service.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("usertoken");
        log.info("usertoken = {}", token);
        String username = JwtUtil.parseValueWithoutException(token, "token");
        if (StringUtils.isEmpty(username)){
            response.getWriter().write("toLogin");
            return false;
        }


        return true;
    }
}
